﻿using System.Collections.Generic;
using System.Xml;

using UCPostData = System.Collections.Generic.Dictionary<string, object>;
using System.Text;
using System;

namespace UCenterLib {
    public class UCXmlSerialization {
        static Encoding GB2312 = Encoding.GetEncoding("GB2312");
        static Encoding ISO_8859_1 = Encoding.GetEncoding("ISO-8859-1");

        public static string Serialize(UCPostData document) {
            StringBuilder sb = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Reset();
            // settings.Encoding = Encoding.GetEncoding("ISO-8859-1");

            XmlWriter writer = XmlWriter.Create(sb, settings);
            
            // writer.WriteStartDocument();
            writer.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"ISO-8859-1\"");
            writer.WriteStartElement("root");
            Serialize(document, writer);
            writer.WriteEndElement();
            writer.WriteEndDocument();

            writer.Flush();
            return sb.ToString();
        }

        static void Serialize(UCPostData document, XmlWriter writer) {
            foreach (string k in document.Keys) {
                
                writer.WriteStartElement("item");
                writer.WriteStartAttribute("id");
                writer.WriteString(k);
                writer.WriteEndAttribute();

                if (document[k] is UCPostData) {
                    Serialize(document[k] as UCPostData, writer);
                }
                else {
                    writer.WriteCData(Convert.ToString(document[k]));
                }
                writer.WriteEndElement();
            }
        }


        public static UCPostData Deserialize(string xmlContent) {
            XmlDocument doc = new XmlDocument();
            try {
                doc.LoadXml(xmlContent);
            }
            catch {
                return null;
            }
            
            // TODO: DO SOME VERIFICATION


            UCPostData result = new UCPostData();
            Deserialize(doc.DocumentElement.ChildNodes, result);
            
            return result;
        }

        static void Deserialize(XmlNodeList nodes, UCPostData document) {
            foreach (XmlNode node in nodes) {
                if (node.HasChildNodes && node.ChildNodes.Count == 1 && node.ChildNodes[0].NodeType == XmlNodeType.CDATA) {
                    // TODO: DO SOME VERIFICATION
                    document.Add(node.Attributes["id"].Value, node.InnerText);
                } else if (node.HasChildNodes && node.ChildNodes[0].Name == "item") {
                    Dictionary<string, object> childDocument = new Dictionary<string, object>();
                    // TODO: DO SOME VERIFICATION
                    document.Add(node.Attributes["id"].Value, childDocument);
                    Deserialize(node.ChildNodes, childDocument);
                } else { /* do nothing */}
            }
        }
    }
}
